//
//  UIView+OADLayout5.m
//  OASDK_Works
//
//  Created by ChenJie on 3/15/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "UIView+OADLayout5.h"
#import <objc/runtime.h>

@implementation UIView (OADLayout5)

//
- (NSString *)layoutName {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setLayoutName:(NSString *)layoutName {
    objc_setAssociatedObject(self, @selector(layoutName), layoutName, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

//
- (void)applyLayout:(OADLayout5 *)layout {
    
    if (layout.isRootNode) {
        layout.containerWidth = CGRectGetWidth(self.superview.bounds);
    }
    
    self.frame = layout.frame;
    NSLog(@"%@: layout.frame: %@", layout.name, NSStringFromCGRect(layout.frame));
    NSLog(@"%@: self.frame: %@", layout.name, NSStringFromCGRect(self.frame));
    
    for (UIView *subview in self.subviews) {
        if (!subview.layoutName) {
            continue;
        }
        
        OADLayout5 *node = [layout nodeWithName:subview.layoutName];
        if (!node) {
            continue;
        }

        [subview applyLayout:node];
    }
}


@end
